var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    relName:'',
    relCard:'',
    relPhone: '',
    buthiddenSubmit: false,
    buthiddenNext: false,
    name: "",
    idCard: "", 
    bandCard: "",
    phone: "",
    code: "",
    verifyCodeTime: "发送验证码"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.onMineInfo()
    this.setData({ relName: options.relName, relCard: options.relCard, relPhone: options.relPhone });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onMineInfo()
  },
  onMineInfo: function () {
    wx.showLoading({ title: '加载中' })
    var that = this
    wx.request({
      method: 'POST',
      url: app.preUrl + 'findByUserId',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 100) {
          that.setData({
            name: res.data.result.cardName,
            idCard: res.data.result.cardNum
          })
          
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          })
        }
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
  //获取真实姓名
  onBandtruename: function (e) {
    var that = this;
    that.setData({ name: e.detail.value });
  },
  //获取身份证号
  onBandidentify: function (e) {
    var that = this;
    that.setData({ idCard: e.detail.value });
  },
  //银行卡号前端校验
  onBandCardEvent: function (e) {
    var that = this;
    that.setData({ bandCard: e.detail.value.replace(/\D/g, '') });
  },
  //手机号前端校验
  onPhoneEvent: function (e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ phone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ phone: e.detail.value.replace(/\D/g, '') });
    }
  },
  //手机号验证码前端校验
  onCodeEvent: function (e) {
    var that = this;
    that.setData({ code: e.detail.value.replace(/\D/g, '') });
  },

  //提交手机号
  onPhoneNext: function () {
    var that = this;
    var phone = that.data.phone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var c = 60;
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
      return false
    } else if (phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      that.setData({ buthiddenNext: false });

      var intervalId = setInterval(function () {
        c = c - 1;
        that.setData({
          verifyCodeTime: c + 's后重发',
          buthiddenNext: true
        })
        if (c == 0) {
          clearInterval(intervalId);
          that.setData({
            verifyCodeTime: '获取验证码',
            buthiddenNext: false
          })
        }
      }, 1000)

      wx.request({
        method: "POST",
        url: app.preUrl + 'obtainMessageBank',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          telephone: that.data.phone,
        },
        success: function (res) {

          if (res.data.code == 100) {
            that.setData({ buthiddenNext: true });
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false,
            });
            clearInterval(intervalId);
            that.setData({
              verifyCodeTime: '获取验证码',
              buthiddenNext: false
            })
          }

        },
        fail: function (error) {
          that.setData({ buthiddenNext: true });
        }
      })
    }
  },
  getPaymetFlag() {
    var that = this
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      method: 'POST',
      url: app.preUrl + 'payPassInfo',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.code != 100) {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '绑定成功,请前往设置支付密码',
            confirmText: '去设置',
            confirmColor: '#56BDE0',
            success(res) {
              if (res.confirm) {
        
                wx.navigateTo({
                  // url: 'checkSetPaymentPassword?phone=' + that.data.relPhone+'&name='+that.data.relName+'&idCard='+that.data.relCard + '&type=2' //2绑定银行卡
                  url: 'checkSetPaymentPassword?phone=' + that.data.phone + '&name=' + that.data.name + '&idCard=' + that.data.bandCard + '&type=2' //2绑定银行卡
                })

              }
            }
          })
        }else{
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 1000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },

  bindFormSubmit: function (e) {
    var that = this;
    var cardName = that.data.name;
    // var cardName = that.data.cardName;
    var cardNum = that.data.idCard;
    // var cardNum = that.data.cardNum; 
    //var name = e.detail.value.name;console.log("name",name);
    //var idCard = e.detail.value.idCard; console.log("idCard", idCard);
    var bandCard = e.detail.value.bandCard;
    var phone = e.detail.value.phone;
    var code = e.detail.value.code;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (cardName == '' || cardNum == '' || bandCard == '' || phone == '' || code == '') {
      wx.showToast({
        title: '各项信息必填',
        icon: "none"
      })
      return false;
    } else if (cardNum.length != 18) {
      wx.showToast({
        title: '身份证长度有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (bandCard.length != 16 && bandCard.length != 17 && bandCard.length != 18 && bandCard.length != 19) {
      wx.showToast({
        title: '银行卡长度有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (code.length != 4) {
      wx.showToast({
        title: '验证码长度有误',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      that.setData({ buthiddenSubmit: true });
      wx.showLoading({
        title: '加载中'
      })
      console.log(wx.getStorageSync('userId'), cardName, cardNum, bandCard, phone, code)
      wx.request({
        method: "POST",
        url: app.preUrl + 'saveBankCard',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          userId: wx.getStorageSync('userId'),
          bankName: cardName,//身份证姓名
          cardNumber: cardNum,//身份证号
          bankCardNumber: bandCard,//银行卡号
          phone: phone,
          code: code,
        },
        success: function (res) {
          console.log(res.data)
          var c = 3;
          // if (res.data.code == 401) {错误的
          if (res.data.code == 100) {

            that.getPaymetFlag()
           

          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false,
            })
            that.setData({ buthiddenNext: false, buthiddenSubmit: false });
            wx.hideLoading()
          }
        },
        fail: function (error) {
          that.setData({ buthiddenSubmit: false });
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})